package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.common;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.RechargeOrderDetialBean;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.math.BigDecimal;

import butterknife.BindView;

/**
 * 充值成功
 */
public class RechargeSuccessFragment extends BaseFragment {
    public static RechargeSuccessFragment newInstance(String orderNumber) {
        RechargeSuccessFragment rechargeSuccessFragment = new RechargeSuccessFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderNumber", orderNumber);
        rechargeSuccessFragment.setArguments(bundle);
        return rechargeSuccessFragment;
    }

    @BindView(R.id.tv_print_two)
    TextView tvPrintTwo;
    @BindView(R.id.tv_order_money)
    TextView tvOrderMoney;
    private String orderNumber;
    private RechargeSuccessMode rechargeSuccessMode;
    private RechargeOrderDetialBean beans;
    @BindView(R.id.tv_payState)
    TextView tv_payState;
    @Override
    protected int initLayout() {
        return R.layout.fg_paysuccess;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.RechargeSuccess), getActivity());
       // tvPrintTwo.setBackground(ViewBgUtil.setBg(360, Color.parseColor(ConfigureFile.ColorSystem)));
        tvPrintTwo.setOnClickListener(view1 -> {
//            rechargeSuccessMode.IsPrintTicket(getContext(),beans,1);
            rechargeSuccessMode.printMemberRechargeTicket(beans,true);
            tvPrintTwo.setVisibility(View.GONE);
        });
        tvPrintTwo.setVisibility(View.VISIBLE);
        tv_payState.setText("充值成功");
    }

    @Override
    protected void initData(Context mContext) {
        rechargeSuccessMode=getViewModelProvider().get(RechargeSuccessMode.class);
        orderNumber = getArguments().getString("orderNumber");
        if (!StringUtils.isEmpty(orderNumber)) {
            rechargeSuccessMode.getOrderDetialData(getContext(),orderNumber);
            //Dayin();
        } else {
            ToastUtils.showToast(getContext(), "订单号异常！");
        }
    }

    @Override
    protected void loadMethod() {
        getOrderDetialData();
    }

    /**
     * 处理订单详情查询结果
     * @param
     */
    private void getOrderDetialData() {
        rechargeSuccessMode.getLiveData().observe(this, bean -> {
            beans=bean;
            String orderAmount = bean.getAmount();
            double orderAmountDou = ConverterHelper.toBigDecimal(orderAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            tvOrderMoney.setText(orderAmountDou + "元");
            rechargeSuccessMode.IsPrintTicket(getContext(),bean,0);
        });
    }
}
